//
//  PayQrCodePopView.m
//  Common
//
//  Created by shiqing guo on 2018/11/8.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "PayQrCodePopView.h"
#import "Macro.h"
#import "Masonry.h"
#import "QRCodeUtil.h"
#include "UIButton+Category.h"
#import "UIView+Category.h"
#import "ToastView.h"
#import "UIView+DDAddition.h"

#define HeightOfPop  344.0f
#define WidthOfPop   264.0f
#define HeightOfQr   160.0f

@interface PayQrCodePopView ()
@property (nonatomic, strong) UIView *viwPop;

@property (nonatomic, strong) UIButton *btnClose;
@property (nonatomic, strong) UIImageView *igvQrCode;
@property (nonatomic, strong) UILabel *lblMessage;
@property (nonatomic, strong) UIButton *btnSaveQrCode;
@end

@implementation PayQrCodePopView

- (instancetype)init
{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    [self addSubview:self.viwPop];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
//    [self addGestureRecognizer:tap];
}


#pragma mark - 视图懒加载
- (UIView *)viwPop
{
    if (!_viwPop) {
        _viwPop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WidthOfPop, HeightOfPop)];
        _viwPop.center = self.center;
        _viwPop.layer.cornerRadius = 10.0f;
        _viwPop.backgroundColor = [UIColor whiteColor];
        
        [_viwPop addSubview:self.btnClose];
        self.btnClose.right = WidthOfPop - 10.0f;
        [_viwPop addSubview:self.igvQrCode];
        self.igvQrCode.centerX = WidthOfPop/2;
        self.igvQrCode.top = 56.0f;
        [_viwPop addSubview:self.lblMessage];
        self.lblMessage.top = self.igvQrCode.bottom + 8.0f;
        self.lblMessage.centerX = WidthOfPop/2;
        
        [_viwPop addSubview:self.btnSaveQrCode];
        self.btnSaveQrCode.width = WidthOfPop-2*Cell_X;
        self.btnSaveQrCode.centerX = WidthOfPop/2;
        self.btnSaveQrCode.bottom = HeightOfPop - Cell_X;
    }
    return _viwPop;
}


- (void)showWithUrl:(NSString *)url andMessage:(NSString *)message
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    
    self.igvQrCode.image = [QRCodeUtil createQRCodeWithString:url qrSize:CGSizeMake(HeightOfQr, HeightOfQr)];
    self.lblMessage.text = message;
}

- (void)dismissView
{
    [self.viwPop removeFromSuperview];
    [self removeFromSuperview];
}


#pragma mark - Button Action
- (void)onCloseBtnClick:(UIButton *)sender
{
    [self dismissView];
}

#pragma mark - 懒加载
- (UIButton *)btnClose
{
    if (!_btnClose) {
        _btnClose = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 30, 30)];
        [_btnClose setImage:[UIImage imageNamed:@"trip_office_close"] forState:UIControlStateNormal];
        [_btnClose addTarget:self action:@selector(onCloseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnClose setEnlargeEdgeWithTop:10.0f right:10.0f bottom:10.0f left:10.0f];
    }
    return _btnClose;
}

- (UIImageView *)igvQrCode
{
    if (!_igvQrCode) {
        _igvQrCode = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HeightOfQr, HeightOfQr)];
    }
    return _igvQrCode;
}


- (UILabel *)lblMessage
{
    if (!_lblMessage) {
        _lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 18.0f)];
        _lblMessage.font = SystemFont(12.0f);
        _lblMessage.textColor = [EPColor epColor_statusMessageGray];
        _lblMessage.textAlignment = NSTextAlignmentCenter;
    }
    return _lblMessage;
}

- (UIButton *)btnSaveQrCode
{
    if (!_btnSaveQrCode) {
        _btnSaveQrCode = [[UIButton alloc] initWithFrame:CGRectMake(Cell_X, 0, 200.0f, 44.0f)];
        [_btnSaveQrCode setBackgroundColor:[EPColor epColor_Blue]];
        [_btnSaveQrCode setTitle:@"保存二维码" forState:UIControlStateNormal];
        [_btnSaveQrCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnSaveQrCode addTarget:self action:@selector(btnSaveQrcodeClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnSaveQrCode.layer.cornerRadius = 4.0f;
        _btnSaveQrCode.layer.masksToBounds = YES;
    }
    return _btnSaveQrCode;
}

#pragma mark - action
- (void)btnSaveQrcodeClick:(id)sender
{
    UIImage *qrCodeImage = [self.igvQrCode screenshot];
    UIImageWriteToSavedPhotosAlbum(qrCodeImage, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [ToastView showMsg:[NSString stringWithFormat:@"%@", error]];
    } else {
        [ToastView showMsg:@"保存成功"];
        [self dismissView];
    }
}

@end
